package com.ziyun.pop.web.modules.storeProductPrice.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.utility.New;
import com.utility.StringUtils;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.storeProductPrice.entity.StoreGoodsProductEntity;
import com.ziyun.pop.modules.storeProductPrice.vo.StorePriceVo;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.pop.modules.storeProductPrice.entity.StoreProductPriceEntity;
import com.ziyun.pop.modules.storeProductPrice.service.StoreProductPriceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 门店产品阶梯价
 *
 * @author xueqz
 * @email 
 * @date 2018-09-26 16:13:59
 */
@RestController
@RequestMapping("storeProductPrice/storeproductprice")
public class StoreProductPriceController {
    @Autowired
    private StoreProductPriceService storeProductPriceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("storeProductPrice:storeproductprice:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = storeProductPriceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{stepId}")
    @RequiresPermissions("storeProductPrice:storeproductprice:info")
    public R info(@PathVariable("stepId") Integer stepId){
        StoreProductPriceEntity storeProductPrice = storeProductPriceService.getById(stepId);

        return R.ok().put("storeProductPrice", storeProductPrice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("storeProductPrice:storeproductprice:save")
    public R save(@RequestBody StoreProductPriceEntity storeProductPrice){
        storeProductPriceService.save(storeProductPrice);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("storeProductPrice:storeproductprice:update")
    public R update(@RequestBody StoreProductPriceEntity storeProductPrice){
        ValidatorUtils.validateEntity(storeProductPrice);
        storeProductPriceService.updateById(storeProductPrice);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("storeProductPrice:storeproductprice:delete")
    public R delete(@RequestBody Integer[] stepIds){
        storeProductPriceService.removeByIds(Arrays.asList(stepIds));
//        storeProductPriceService.flagDelete(stepIds);
        return R.ok();
    }

    /**
     * 分类树
     */
    @RequestMapping("/storeCategoryTree")
    public R storeCategoryTree(){
        Map<String, Object> resultMap  = storeProductPriceService.getStoreCategoryTree();
        return R.ok().put("storeCategoryTree",resultMap.get("storeCategoryTree"));
    }

//
    /**
     * 根据门店分类找该分类下的所有商品(门店id用来匹配含价格)
     */
    @RequestMapping("/getStoreProductByCatId/{catId}")
    public R getStoreProductByCatId(@PathVariable("catId") Integer catId){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        List<StoreGoodsProductEntity> storeGoodsProductEntityList = storeProductPriceService.getStoreProductByCatId(catId,sellerId);
        return R.ok().put("storeGoodsProductEntityList",storeGoodsProductEntityList);
    }

    /**
     * 根据门店分类找该分类下的所有商品(门店id用来匹配含价格)
     */
    @RequestMapping("/searchByName/{queryName}")
    public R searchByName(@PathVariable("queryName") String queryName){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        List<StoreGoodsProductEntity> storeGoodsProductEntityList = storeProductPriceService.getStoreProductByName(queryName,sellerId);
        return R.ok().put("storeGoodsProductEntityList",storeGoodsProductEntityList);
    }

    /**
     * createStorePrice
     * 为用户创建的价格和阶梯价做保存
     */
    @RequestMapping("/createStorePrice")
    public R createStorePrice(@RequestBody StorePriceVo storePriceVo){
        return storeProductPriceService.editPrice(storePriceVo);
//
//
//        String stepPrice = storePriceVo.getStepPrice();
//        StoreGoodsProductEntity storeGoodsProductEntity = storePriceVo.getStoreGoodsProductEntity();
//        //需要先把该门店下的该产品标记删除,再添加
//        storeProductPriceService.editFlagDelete(storeGoodsProductEntity.getProductId(),UserUtils.getCurentUser().getSellerId());
//        String marks = storePriceVo.getMarks();
//        StoreProductPriceEntity storeProductPriceEntity = new StoreProductPriceEntity();
//        storeProductPriceEntity.setProductId(storeGoodsProductEntity.getProductId());
//        storeProductPriceEntity.setSellerId(UserUtils.getCurentUser().getSellerId());
//        storeProductPriceEntity.setPrice(storeGoodsProductEntity.getStorePrice());
//        storeProductPriceEntity.setStepPrice(stepPrice);
//        storeProductPriceEntity.setMarks(marks);
//        storeProductPriceEntity.setDel(false);
//        storeProductPriceEntity.setCreateName(UserUtils.getCurentUser().getUsername());
//        boolean save = storeProductPriceService.save(storeProductPriceEntity);
//        if(save){
//            return R.ok();
//        }else{
//            return R.error("保存价格失败");
//        }
    }

    /**
     * 根据门店分类找该分类下的所有商品(门店id用来匹配含价格)
     */
    @RequestMapping("/getStoreProductByProductId/{productId}")
    public R getStoreProductByProductId(@PathVariable("productId") Integer productId){
        Long sellerId = UserUtils.getCurentUser().getSellerId();
        QueryWrapper<StoreProductPriceEntity> eq = new QueryWrapper<StoreProductPriceEntity>()
                .eq(productId != null, "product_id", productId)
                .eq(sellerId != null, "seller_id", sellerId)
                .eq("del", false);
        StoreProductPriceEntity storeProductPriceEntity = storeProductPriceService.getOne(eq);
        Map<String, Object> resultMap = New.hashMap();
        if(storeProductPriceEntity != null){
            resultMap.put("priceInput",storeProductPriceEntity.getPrice());
            resultMap.put("marksInput",storeProductPriceEntity.getMarks());
            String stepPrice = storeProductPriceEntity.getStepPrice();
            resultMap.put("stepPriceList",JSONArray.parseArray(stepPrice));
            resultMap.put("startPriceInput",storeProductPriceEntity.getStartPrice());
        }
        return R.ok().put("resultMap",resultMap);
    }
}
